package cn.zzxy.day01;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;

public class Demo4 {
    public static void main(String[] args) throws Exception{
        //加载驱动
        Class.forName("com.mysql.cj.jdbc.Driver");
        //获取连接对象
        Connection connection = DriverManager.getConnection(
                "jdbc:mysql://localhost:3306/exam?serverTimezone=UTC",
                "root","root");
        //创建statement对象
        Statement statement = connection.createStatement();
        //sql语句
        String sql = "select * from dept";
        //执行sql，返回一个结果集合
        ResultSet rs = statement.executeQuery(sql);
        //遍历结果集,rs.next()如果结果集中有数据就返回true，如果没数据了就返回false
        while(rs.next()){
            String deptno = rs.getString("deptno");
            String dname = rs.getString("dname");
            String loc = rs.getString("loc");
            System.out.println(deptno+","+dname+","+loc);
        }
        //关闭连接
        connection.close();
    }
}
